package com.example.myapplication.tack_09;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.tack_09.adapter.ArrangeAdapter;
import com.example.myapplication.tack_09.adapter.FenLeiAdapter;
import com.example.myapplication.tack_09.data.ArrangeData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;

import java.util.List;

public class FenleiActivity extends AppCompatActivity {
    private ImageView exit;
    private List<ArrangeData.RowsDTO> fenLeiList;
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_09_activity_fenlei);
        init();
        exit_();
        Intent intent = getIntent();
        String stringExtra = intent.getStringExtra(Common.COMMON);
        getList(stringExtra);
    }

    private void getList(String stringExtr) {
        String url = "/prod-api/api/public-welfare/public-welfare-activity/list?" +
                "typeId="+stringExtr+"&pageNum=1&pageSize=5";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ArrangeData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ArrangeData arrangeData = (ArrangeData)obj;
                fenLeiList = arrangeData.getRows();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        listAdapter(stringExtr);
                    }
                });

            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void listAdapter(String stringExtr) {
        ArrangeAdapter arrangeAdapter = new ArrangeAdapter(fenLeiList, FenleiActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(FenleiActivity.this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(arrangeAdapter);
    }


    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FenleiActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        recyclerView = findViewById(R.id.list);
    }
}